package com.example.espressotests

import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_my_list.*
import kotlinx.android.synthetic.main.activity_my_list_adapter.view.*

class MyListActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_list)

        initView()
    }

    private fun initView() {
        recycle_view.layoutManager = LinearLayoutManager(this)
        recycle_view.adapter = MyAdapter(
            this,
            arrayListOf(
                "第一列",
                "第二列",
                "第三列",
                "第四列",
                "第五列",
                "第六列",
                "第七列",
                "第八列",
                "第九列",
                "第十列",
                "第十一列",
                "第十二列",
                "第十三列",
                "第十四列",
                "第十五列",
                "第十六列",
                "第十七列",
                "第十八列",
                "第十九列",
                "第二十列"
            ), object : MyAdapter.ItemClickListener {
                override fun onItemClickListener(position: Int, itemValue: String) {
                    val alertDialog = AlertDialog.Builder(this@MyListActivity)
                    alertDialog.setTitle("标题")
                    alertDialog.setPositiveButton(
                        "确定"
                    ) { _: DialogInterface, _: Int ->
                        Toast.makeText(this@MyListActivity, "itemValue: 确定", Toast.LENGTH_SHORT)
                            .show()
                    }

                    alertDialog.setNegativeButton("取消") { _: DialogInterface, _: Int ->
                        Toast.makeText(this@MyListActivity, "itemValue: 取消", Toast.LENGTH_SHORT)
                            .show()
                    }
                    alertDialog.show()
                }
            }
        )
    }

    class MyAdapter(
        private val context: Context,
        private val data: List<String>,
        private val itemClickListener: ItemClickListener
    ) :
        RecyclerView.Adapter<MyAdapter.MyHolder>() {


        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
            return MyHolder(
                LayoutInflater.from(context)
                    .inflate(R.layout.activity_my_list_adapter, parent, false)
            )
        }

        override fun getItemCount(): Int {
            return data.size
        }

        override fun onBindViewHolder(holder: MyHolder, position: Int) {
            holder.itemValue.text = data[position]
            holder.itemValue.setOnClickListener {
                itemClickListener.onItemClickListener(position, data[position])
            }
        }

        inner class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var itemValue: AppCompatTextView = itemView.item_value
        }


        interface ItemClickListener {
            fun onItemClickListener(position: Int, itemValue: String)
        }
    }
}


